Import: Importing no longer accepts too big revisions
[lhc/web/wiklou.git] / includes / Import.php
index f0576d7..67f7ba5 100644 (file)
@@ -660,14 +660,26 @@ class WikiImporter {
         * @return bool|mixed
         */
        private function processLogItem( $logInfo ) {
+
                $revision = new WikiRevision( $this->config );
 
-               $revision->setID( $logInfo['id'] );
+               if ( isset( $logInfo['id'] ) ) {
+                       $revision->setID( $logInfo['id'] );
+               }
                $revision->setType( $logInfo['type'] );
                $revision->setAction( $logInfo['action'] );
-               $revision->setTimestamp( $logInfo['timestamp'] );
-               $revision->setParams( $logInfo['params'] );
-               $revision->setTitle( Title::newFromText( $logInfo['logtitle'] ) );
+               if ( isset( $logInfo['timestamp'] ) ) {
+                       $revision->setTimestamp( $logInfo['timestamp'] );
+               }
+               if ( isset( $logInfo['params'] ) ) {
+                       $revision->setParams( $logInfo['params'] );
+               }
+               if ( isset( $logInfo['logtitle'] ) ) {
+                       // @todo Using Title for non-local titles is a recipe for disaster.
+                       // We should use ForeignTitle here instead.
+                       $revision->setTitle( Title::newFromText( $logInfo['logtitle'] ) );
+               }
+
                $revision->setNoUpdates( $this->mNoUpdates );
 
                if ( isset( $logInfo['comment'] ) ) {
@@ -677,7 +689,10 @@ class WikiImporter {
                if ( isset( $logInfo['contributor']['ip'] ) ) {
                        $revision->setUserIP( $logInfo['contributor']['ip'] );
                }
-               if ( isset( $logInfo['contributor']['username'] ) ) {
+
+               if ( !isset( $logInfo['contributor']['username'] ) ) {
+                       $revision->setUsername( 'Unknown user' );
+               } else {
                        $revision->setUserName( $logInfo['contributor']['username'] );
                }
 
@@ -810,6 +825,30 @@ class WikiImporter {
         * @return bool|mixed
         */
        private function processRevision( $pageInfo, $revisionInfo ) {
+               global $wgMaxArticleSize;
+
+               // Make sure revisions won't violate $wgMaxArticleSize, which could lead to
+               // database errors and instability. Testing for revisions with only listed
+               // content models, as other content models might use serialization formats
+               // which aren't checked against $wgMaxArticleSize.
+               if ( ( !isset( $revisionInfo['model'] ) ||
+                       in_array( $revisionInfo['model'], array(
+                               'wikitext',
+                               'css',
+                               'json',
+                               'javascript',
+                               'text',
+                               ''
+                       ) ) ) &&
+                       (int)( strlen( $revisionInfo['text'] ) / 1024 ) > $wgMaxArticleSize
+               ) {
+                       throw new MWException( 'The text of ' .
+                               ( isset( $revisionInfo['id'] ) ?
+                                       "the revision with ID $revisionInfo[id]" :
+                                       'a revision'
+                               ) . " exceeds the maximum allowable size ($wgMaxArticleSize KB)" );
+               }
+
                $revision = new WikiRevision( $this->config );
 
                if ( isset( $revisionInfo['id'] ) ) {
@@ -846,9 +885,10 @@ class WikiImporter {
                }
                if ( isset( $revisionInfo['contributor']['ip'] ) ) {
                        $revision->setUserIP( $revisionInfo['contributor']['ip'] );
-               }
-               if ( isset( $revisionInfo['contributor']['username'] ) ) {
+               } elseif ( isset( $revisionInfo['contributor']['username'] ) ) {
                        $revision->setUserName( $revisionInfo['contributor']['username'] );
+               } else {
+                       $revision->setUserName( 'Unknown user' );
                }
                $revision->setNoUpdates( $this->mNoUpdates );
 
@@ -966,6 +1006,9 @@ class WikiImporter {
                $fields = array( 'id', 'ip', 'username' );
                $info = array();
 
+               if ( $this->reader->isEmptyElement ) {
+                       return $info;
+               }
                while ( $this->reader->read() ) {
                        if ( $this->reader->nodeType == XMLReader::END_ELEMENT &&
                                        $this->reader->localName == 'contributor' ) {
@@ -1499,7 +1542,7 @@ class WikiRevision {
         */
        function getSha1() {
                if ( $this->sha1base36 ) {
-                       return wfBaseConvert( $this->sha1base36, 36, 16 );
+                       return Wikimedia\base_convert( $this->sha1base36, 36, 16 );
                }
                return false;
        }
@@ -1655,6 +1698,16 @@ class WikiRevision {
 
        function importLogItem() {
                $dbw = wfGetDB( DB_MASTER );
+
+               $user = User::newFromName( $this->getUser() );
+               if ( $user ) {
+                       $userId = intval( $user->getId() );
+                       $userText = $user->getName();
+               } else {
+                       $userId = 0;
+                       $userText = $this->getUser();
+               }
+
                # @todo FIXME: This will not record autoblocks
                if ( !$this->getTitle() ) {
                        wfDebug( __METHOD__ . ": skipping invalid {$this->type}/{$this->action} log time, timestamp " .
@@ -1687,8 +1740,8 @@ class WikiRevision {
                        'log_type' => $this->type,
                        'log_action' => $this->action,
                        'log_timestamp' => $dbw->timestamp( $this->timestamp ),
-                       'log_user' => User::idFromName( $this->user_text ),
-                       # 'log_user_text' => $this->user_text,
+                       'log_user' =>  $userId,
+                       'log_user_text' => $userText,
                        'log_namespace' => $this->getTitle()->getNamespace(),
                        'log_title' => $this->getTitle()->getDBkey(),
                        'log_comment' => $this->getComment(),